package com.gonsin.easycount.model;

import com.gonsin.easycount.exception.EasyCountException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class CountResult {

    /**
     * 统计总数
     */
    private Long sum;

    /**
     * 统计选项数量
     */
    private Map<String, Long> sumOptions = new HashMap<>();

//    /**
//     * 统计结果
//     */
//    private Map<String, Long> sumUserInfos = new HashMap<>();

    /**
     * 距离上一次数据变更时新增加的用户
     */
    private List<UserInfoOption> newUserInfos;

    /**
     * 所有人员信息
     */
    private Set<UserInfoOption> allUserInfos;

    /**
     * 获取holder内容
     */
    private Object holder;

    /**
     * 本次结果输出是否由手动flush触发。
     */
    private boolean byFlush;

    /**
     * 获取总票结果
     */
    public Long getSum() {
        if(sum == null){
            throw new EasyCountException("调用getSum前请先调用CountGetter.needSum()方法");
        }
        return sum;
    }

    public Long getSumOptions(String option) {
        if(sumOptions == null){
            throw new EasyCountException("调用getSumOptions前请先调用CountGetter.needSumOptions()方法");
        }
        Long result = sumOptions.get(option);
        if(result == null){
            throw new EasyCountException("调用getSumOptions前请先调用CountGetter.needSumOptions()方法，或者请检查needSumOptions的参数是否和getSumOptions的参数相同");
        }
        return result;
    }

//    public Long getSumUserInfos(String userInfo) {
//        if(sumUserInfos == null){
//            throw new EasyCountException("调用getSumUserInfos前请先调用CountGetter.needSumUserInfos()方法");
//        }
//        Long result = sumUserInfos.get(userInfo);
//        if(result == null){
//            throw new EasyCountException("调用getSumUserInfos前请先调用CountGetter.needSumUserInfos()方法，或者请检查needSumUserInfos的参数是否和getSumUserInfos的参数相同");
//        }
//        return result;
//    }

    public <T> List<UserInfoOption<T>> getNewUserInfos() {
        if(newUserInfos == null){
            throw new EasyCountException("调用getNewUserInfos前请先调用CountGetter.needNewUserInfos()方法");
        }
        return newUserInfos.stream().map(u -> (UserInfoOption<T>)u).collect(Collectors.toList());
    }

    public void setSum(Long sum) {
        this.sum = sum;
    }

    public void addSumOption(String option, Long sumOptions) {
        this.sumOptions.put(option, sumOptions);
    }

//    public void addSumUserInfos(String userInfo, Long sumUserInfos) {
//        this.sumUserInfos.put(userInfo, sumUserInfos);
//    }

    public void setNewUserInfos(List<UserInfoOption> newUserInfos) {
        this.newUserInfos = newUserInfos;
    }

    public <T> T getHolder() {
        if(holder == null){
            throw new EasyCountException("调用getHolder前请先调用CountGetter.needHolder()方法");
        }
        return (T) holder;
    }

    public void setHolder(Object holder) {
        this.holder = holder;
    }

    public boolean isByFlush() {
        return byFlush;
    }

    public void setByFlush(boolean byFlush) {
        this.byFlush = byFlush;
    }

    public <T> Set<UserInfoOption<T>> getAllUserInfos() {
        if(allUserInfos == null){
            throw new EasyCountException("调用getAllUserInfos前请先调用CountGetter.needAllUserInfos()方法");
        }
        return allUserInfos.stream().map(u -> (UserInfoOption<T>)u).collect(Collectors.toSet());
    }

    public void setAllUserInfos(Set<UserInfoOption> allUserInfos) {
        this.allUserInfos = allUserInfos;
    }
}
